﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Shared.Service.DataPermissions
{
    /// <summary>
    /// 特殊数数据权限 使用场景 客户列表模块
    /// 使用原因：
    /// </summary>
    public class SpecialDataPermission
    {
        /// <summary>
        /// 没启用的
        /// </summary>
        public static SpecialDataPermission Disable = new SpecialDataPermission(DataPermissionState.Disable);

        /// <summary>
        /// 没启用的
        /// </summary>
        public static SpecialDataPermission Null = new SpecialDataPermission(DataPermissionState.Null);

        /// <summary>
        /// 是否是所有
        /// </summary>
        public static SpecialDataPermission All = new SpecialDataPermission(DataPermissionState.All);

        /// <summary>
        /// 
        /// </summary>
        public DataPermissionState State { get; }

        public string[] AllSchoolIds { get; set; }

        public string[] CompleteSchoolIds { get; set; }

        /// <summary>
        /// 权限范围内门店Id
        /// </summary>
        public string[] UnCompleteSchoolIds { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string[] DepartmentIds { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        protected SpecialDataPermission(DataPermissionState state)
        {
            State = state;
        }

        public SpecialDataPermission(string[] allSchoolIds, string[] completeSchoolIds,
            string[] unCompleteSchoolIds, string[] departmentIds = null)
            : this(DataPermissionState.Normal)
        {
            AllSchoolIds = allSchoolIds;
            CompleteSchoolIds = completeSchoolIds;
            UnCompleteSchoolIds = unCompleteSchoolIds;
            DepartmentIds = departmentIds;
        }

        public List<string> TryGetAllSchoolIds(string schoolId)
        {
            if (State != DataPermissionState.Normal)
            {
                return schoolId.IsNullOrWhiteSpace() ? new List<string>() : new List<string> { schoolId };
            }

            // 如果已存在
            if (AllSchoolIds.Any(a => a == schoolId))
            {
                return AllSchoolIds.ToList();
            }

            var allSchoolIds = AllSchoolIds.ToList();
            if (!schoolId.IsNullOrWhiteSpace())
            {
                allSchoolIds.Add(schoolId);
            }

            return allSchoolIds;
        }
    }

}
